--[[ 编码: WMS-04-03 名称: 批量导入 作者:HAN 日期:2025-1-29 入口函数:ImportLocation 功能说明: 处理货位导入,在导入的时候判断一下仓库、库区等是否存在?如果不存在要新创建 导入格式见《#HHWMS-001 仓库货位定义表.xlsx》 变更历史: V2.0 HAN 20250330 适合 WMS Basis 版本V15.5以上的数据模型版本 -- 增加对巷道数据对的创建 -- Error 用 stopProgram 替换 --]] wms_base = require ("wms_base") local location_type = {} -- 货位类型 local purpose = {} -- 用途 -- 获取字典项中的Name,并且转成数值返回 local function GetLocationTyeValue( strLocTypeName ) local n, nCount local nValue = 1 nCount = #location_type for n = 1, nCount do if ( location_type[n].Value == strLocTypeName ) then nValue = lua.StrToNumber( location_type[n].Name ) if ( nValue == 0 ) then nValue = 1 end return nValue end end return nValue end -- 获取字典项中的Name,并且转成数值返回 local function GetPurposeValue( strPurpose ) local n, nCount local nValue = 0 nCount = #purpose for n = 1, nCount do if ( purpose[n].Value == strPurpose ) then return lua.StrToNumber( purpose[n].Name ) end end return nValue end function ImportLocation(strLuaDEID) local nRet, strRetInfo -- 获取导入的数据, 返回 [{"attr":"xx","value":""},...] local row_data = {} nRet, row_data = m3.GetSysDataJson(strLuaDEID) if (nRet ~= 0 or strRetInfo == '') then mobox.stopProgram( strLuaDEID, "无法获取导入数据!") return end local row_attrs local n, nCount, nValue, nRows nRows = #row_data if ( nRows == 0 ) then return end -- 获取操作者的工厂标识 local factory nRet, factory = m3.GetMyFactory( strLuaDEID ) if ( nRet ~= 0 ) then mobox.stopProgram( strLuaDEID, "GetMyFactory失败! "..factory ) return end if ( factory == '' ) then factory = '0000' end -- 一些关键属性 local strWHCode = '' local strWHName = '' local strAreaCode = '' local strAreaName = '' local strLocCode = '' local strLocName = '' local location = {} -- 获取 货位类型 字典,把 输入 的“常规/堆叠” 转成 整数值 nRet, strRetInfo = mobox.getDictItemIInfo( "WMS_LocationType" ) if ( nRet ~= 0 or strRetInfo == '') then mobox.stopProgram( strLuaDEID, "系统没有定义WMS_LocationType字典!" ) return end location_type = json.decode( strRetInfo ) nRet, strRetInfo = mobox.getDictItemIInfo( "WMS_LocationPurpose" ) if ( nRet ~= 0 or strRetInfo == '') then mobox.stopProgram( strLuaDEID, "系统没有定义WMS_LocationPurpose字典!") return end purpose = json.decode( strRetInfo ) -- 步骤1 获取从excel导入的一行数据 for row = 1, nRows do row_attrs = row_data[row] nCount = #row_attrs strWHCode = '' strWHName = '' strAreaCode = '' strAreaName = '' strLocCode = '' strLocName = '' -- 初始化 货位 对象 location = m3.AllocObject(strLuaDEID,"Location") for n = 1, nCount do strAttr = row_attrs[n].attr strValue = row_attrs[n].value if (strAttr ~= '') then -- 根据导入的excel列头名称进行判断 -- 关键属性判断,如果属性不存在要报错 if (strAttr == "仓库编码") then if (strValue == '') then mobox.stopProgram( strLuaDEID, strAttr .. "不能为空!") return end strWHCode = strValue location.wh_code = strWHCode --V1.2 elseif (strAttr == "仓库名称") then strWHName = strValue elseif (strAttr == "库区编码") then if (strValue == '') then mobox.stopProgram( strLuaDEID, strAttr .. "不能为空!" ) return end strAreaCode = strValue location.area_code = strAreaCode elseif (strAttr == "库区名称") then strAreaName = strValue elseif (strAttr == "货位编码") then -- 货位编码允许为空,如果空用创建前脚本实现编码 strLocCode = strValue location.code = strValue elseif (strAttr == "货位名称") then strLocName = strValue location.name = strValue -- 常规属性 elseif (strAttr == "巷道") then location.aisle = lua.StrToNumber( strValue ) elseif (strAttr == "排") then location.row = lua.StrToNumber( strValue ) elseif (strAttr == "列") then location.col = lua.StrToNumber( strValue ) elseif (strAttr == "层") then location.layer = lua.StrToNumber( strValue ) elseif (strAttr == "货位类型") then location.loc_type = GetLocationTyeValue( strValue ) elseif (strAttr == "用途") then location.purpose = GetPurposeValue( strValue ) elseif (strAttr == "容量") then location.capacity = lua.StrToNumber( strValue ) elseif (strAttr == "长度") then location.length = lua.StrToNumber( strValue ) elseif (strAttr == "宽度") then location.width = lua.StrToNumber( strValue ) elseif (strAttr == "高度") then location.height = lua.StrToNumber( strValue ) --V1.2 elseif (strAttr == "AGV对照编码") then location.agv_site = strValue end end end -- 步骤2 根据货位编码来判断导入的货位是否已经存在 -- 如果已经存在,根据导入的数据进行覆盖 -- 如果不存在需要创建 local attrs local strCondition = "S_CODE='" .. strLocCode .. "'" nRet, strRetInfo = mobox.existThisData(strLuaDEID, "Location", strCondition) if (nRet ~= 0) then mobox.stopProgram( strLuaDEID, "在检查货位是否存在时失败! " .. strRetInfo ) return end if (strRetInfo == 'yes') then -- 已经存在,根据导入的数据进行覆盖 strCondition = "S_CODE='" .. strLocCode .. "'" strSetSQL = "S_WH_CODE = '" ..strWHCode .."' , S_AREA_CODE = '" .. strAreaCode .. "' , S_CODE = '" ..strLocCode .."' , S_NAME = '" .. strLocName .. "' , N_AISLE = '" ..location.aisle.. "' , N_ROW = '" ..location.row.. "' , N_COL = '" .. location.col.. "' , N_LAYER = '" .. location.layer .. "' , N_TYPE = '" .. location.loc_type .. "' , N_CAPACITY ='" .. location.capacity .. "', N_LENGTH = '" .. location.length .. "' , N_WIDTH = '" .. location.width .. "' , N_HEIGHT = '" .. location.height .. "' , S_AGV_SITE = '" .. location.agv_site .. "' " nRet, strRetInfo = mobox.updateDataAttrByCondition(strLuaDEID, "Location", strCondition, strSetSQL) if (nRet ~= 0) then mobox.stopProgram( strLuaDEID, strRetInfo ) return end return end -- 导入的货位不存在需要创建 -- 在新增货位前先判断一下仓库、库区是否存在,如果不存在需要创建 -- 判断仓库是否存在 strCondition = "S_CODE='" .. strWHCode .. "'" nRet, strRetInfo = mobox.existThisData(strLuaDEID, "Warehouse", strCondition) if (nRet ~= 0) then mobox.stopProgram( strLuaDEID, "在检查仓库是否存在时失败! " .. strRetInfo) return end if (strRetInfo == 'no') then -- 新增仓库 if (strWHName == '') then strWHName = strWHCode end local warehouse = m3.AllocObject(strLuaDEID,"Warehouse") warehouse.code = strWHCode warehouse.name = strWHName warehouse.factory = factory nRet, strRetInfo = m3.CreateDataObj( strLuaDEID, warehouse ) if ( nRet ~= 0 ) then mobox.stopProgram( strLuaDEID, 'mobox 创建【仓库】对象失败!'..strRetInfo ) return end end -- 判断库区是否存在 strCondition = "S_WH_CODE='" .. strWHCode .. "' AND S_CODE = '" .. strAreaCode .. "'" nRet, strRetInfo = mobox.existThisData(strLuaDEID, "Area", strCondition) if (nRet ~= 0) then mobox.stopProgram( strLuaDEID, "在检查库区是否存在时失败! " .. strRetInfo) return end --mobox.writeSysLog("strRetInfo",strRetInfo) if (strRetInfo == 'no') then -- 新增库区 if (strAreaName == '') then strAreaName = strAreaCode end local area = m3.AllocObject(strLuaDEID,"Area") area.code = strAreaCode area.name = strAreaName area.wh_code = strWHCode area.factory = factory nRet, area = m3.CreateDataObj( strLuaDEID, area ) if ( nRet ~= 0 ) then mobox.stopProgram( strLuaDEID, 'mobox 创建【库区】对象失败!'..area ) return end if ( location.loc_type == 0 ) then location.loc_type = area.loc_type end end if (location.loc_type == 0 ) then location.loc_type = 1 end -- 如果没定义货位用途默认存储 if (location.purpose == 0 ) then location.purpose = 1 end -- 如果有巷道需要创建巷道数据对象 if ( location.aisle > 0 ) then nRet, strRetInfo = wms.wms_GetAisleInfo( strWHCode, strAreaCode, location.aisle ) if ( nRet ~= 0 ) then mobox.stopProgram( strLuaDEID, '获取巷道信息时失败!'..strRetInfo ) return end local aisle_obj = {} if ( strRetInfo ~= '' ) then aisle_obj = json.decode( strRetInfo ) else -- 巷道不存在,需要创建一个巷道数据对象 local aisle_obj = m3.AllocObject(strLuaDEID,"Aisle") aisle_obj.wh_code = strWHCode aisle_obj.area_code = strAreaCode aisle_obj.aisle = locatio.aisle nRet, aisle_obj = m3.CreateDataObj(strLuaDEID, aisle) if ( nRet ~= 0 ) then mobox.stopProgram( strLuaDEID, '创建巷道时失败!'..aisle_obj ) return end end location.aisle_code = aisle.aisle_code end -- 创建货位 nRet, strRetInfo = m3.CreateDataObj( strLuaDEID, location ) if (nRet ~= 0) then mobox.stopProgram( strLuaDEID, "创建货位失败! " .. strRetInfo ) return end end end